package com.egg.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.egg.action.common.BaseAction;
import com.egg.common.Constants;
import com.egg.service.DeptService;
import com.egg.service.UserService;
/**
 * @项目名称：egg   
 * @类名称：UserAction   
 * @类描述：用户管理action   
 * @创建人：wangchenyang
 * @创建时间：2012-8-31 上午10:46:01   
 * @修改人：
 * @修改时间：   
 * @修改备注：   
 * @version
 */
@ParentPackage("default")
@Namespace("/user")
public class UserAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private List<Map<String,Object>> userList;
	private List<Map<String,Object>> deptList;
	private String deptId;
	private String userId;
	private String userAccount;
	private String userName;
	private String userPassword;
	private String userSex;
	private String userType;
	private String userPhone;
	@Autowired
	UserService userService;
	@Autowired
	DeptService deptService;
	@Action(value="preUpdateUser",results={@Result(name="success",location="/system/user/addOrUpdateUser.jsp")})
	public String preUpdateUser(){
		deptList=deptService.getAllDeptForSelect();
		userList=userService.getOneUser(userId);
		return "success";
	}
	
	@Action(value="preAddUser",results={@Result(name="success",location="/system/user/addOrUpdateUser.jsp")})
	public String preAddUser(){
		deptList=deptService.getAllDeptForSelect();
		return "success";
	}
	
	@Action(value="getAllUser", results = { @Result(name = "success", location = "/system/user/listUser.jsp")})
	public String getAllUser(){
		String userType=String.valueOf(getSession().getAttribute("userType"));
		if(Constants.USER_TYPE_SUPERSYSTEM.equals(userType)){
			userList=userService.getAllSystemManager(Constants.USER_TYPE_SYSTEM);
		}else{
			String userId=String.valueOf(getSession().getAttribute("userId"));
			String deptId=String.valueOf(getSession().getAttribute("deptId"));
			userList=userService.getAllUser(deptId,userId);
		}
		return "success";
	}
	
	@Action(value="addOrUpdateUser", results = { @Result(name = "redirect",type="redirect", location = "getAllUser.action")})
	public String addOrUpdateUser(){
		String sessionUserType=getSession().getAttribute("userType").toString();
		if(!Constants.USER_TYPE_SUPERSYSTEM.equals(sessionUserType)){//如果当前用户不是超级管理员，所添加用户默认是当前部门，用户类型是普通人员
			deptId=getSession().getAttribute("deptId").toString();
			userType=Constants.USER_TYPE_COMMON;
		}else{//是超级管理员，用户类型是系统管理员，部门是页面选择的部门
			userType=Constants.USER_TYPE_SYSTEM;
		}
		if(userId!=null&&!userId.equals("")){
			userService.updateUser(userAccount,userName,userPassword,userSex,userType,deptId,userPhone,userId);
		}else{
			userService.addUser(userAccount,userName,userPassword,userSex,userType,deptId,userPhone,new Date());
		}
		return "redirect";
	}

	@Action(value="deleteUser",results={@Result(name="redirect",location="getAllUser.action",type="redirect")})
	public String deleteUser(){
		userService.deleteUser(userId);
		return "redirect";
	}

	public List<Map<String, Object>> getUserList() {
		return userList;
	}

	public void setUserList(List<Map<String, Object>> userList) {
		this.userList = userList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public String getUserSex() {
		return userSex;
	}

	public void setUserSex(String userSex) {
		this.userSex = userSex;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getUserPhone() {
		return userPhone;
	}

	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public List<Map<String, Object>> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Map<String, Object>> deptList) {
		this.deptList = deptList;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	
}
